package cn.nfwx.tv.fragment;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;

import cn.nfwx.tv.R;
import cn.nfwx.tv.base.BaseFragment;
import cn.nfwx.tv.base.NFWXApplication;
import cn.nfwx.tv.okhttp.UrlHelper;
import cn.nfwx.tv.util.AppUtil;
import cn.nfwx.tv.util.DensityUtil;
import cn.nfwx.tv.util.ZXingUtils;

public class ShareFragment extends BaseFragment implements View.OnClickListener {

    public static ShareFragment newInstance() {
        Bundle args = new Bundle();
        ShareFragment fragment = new ShareFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View initView() {
        View view = View.inflate(mContext, R.layout.dialog_share_friend, null);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (view.findViewById(R.id.share_to_friend) != null) {
            view.findViewById(R.id.share_to_friend).setOnClickListener(this);
        }
        if (view.findViewById(R.id.share_to_wechat) != null) {
            view.findViewById(R.id.share_to_wechat).setOnClickListener(this);
        }
        ImageView iv_share = view.findViewById(R.id.iv_share);
        int width = DensityUtil.dip2px(view.getContext(), 120);
        Bitmap bitmap = ZXingUtils.createQRImage(UrlHelper.getAppqrcode(), width, width);
        iv_share.setImageBitmap(bitmap);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.share_to_friend:
                share(SendMessageToWX.Req.WXSceneTimeline);
                break;
            case R.id.share_to_wechat:
                share(SendMessageToWX.Req.WXSceneSession);
                break;
        }
    }

    public void share(int scene) {
        if (getActivity() != null && !getActivity().isFinishing()) {
            String title = "手机看电视新姿势，南方无线电视邀您一起炫！";
            String des = "看电视直播不卡顿，不延时，还不要钱，你不下，我下了！";
            Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(), R.mipmap.ic_launcher);
            AppUtil.shareWebPage2WeiXin(getContext(), scene, UrlHelper.getAppLink(), title, des, bitmap);
        }
    }
}
